//
// Created by dxyinme on 2021/10/4.
//
#include "gtest/gtest.h"
#include "v_sort_list.h"
#include "v_sort_array.h"
#include "util.h"

#include "v_sort_test_comm.h"

class VsortTestSmallCase : public VsortTestComm {
  void SetUp() override {
    VsortTestComm::SetUp();
  }

  void TearDown() override {
    VsortTestComm::TearDown();
  }
};

TEST_F(VsortTestSmallCase, testVsort12) {
  int len = 12;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, 4, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTestSmallCase, testVsort5) {
  int len = 5;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, len, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTestSmallCase, testVsort5000) {
  int len = 5000;
  auto* o = Vsort::NewRandomIntVList(len);
  auto* res = Vsort::SortVListExThread(o, len, VsortTestComm::GetThreadPoolInstance());
  auto* test_ordered = res;
  while (test_ordered->nxt != nullptr) {
    EXPECT_LE(test_ordered->val, test_ordered->nxt->val);
    test_ordered = test_ordered->nxt;
  }
}

TEST_F(VsortTestSmallCase, testVsortArray5000) {
  int len = 5000;
  int* a = new int[len];
  for (int i = 0; i < len; i ++) {
    a[i] = i;
  }
  std::shuffle(a, a + len, std::mt19937(std::random_device()()));
  Vsort::SortArrayExThread(a, len, VsortTestComm::GetThreadPoolInstance(), std::less<>());
  for (int i = 0; i + 1 < len; i ++) {
    EXPECT_LE(a[i],a[i+1]);
  }
}